package com.css.fxfzdpzs.modules.fwjg.service;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdpzs.modules.fwjg.entity.first.*;
import com.css.fxfzdpzs.modules.fwjg.entity.second.Fwjg5yearsSubmitSituation;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgProjectSituation;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgProjectType;
import com.css.fxfzdpzs.modules.fwjg.entity.second.FwjgSubmitNum;
import com.css.fxfzdpzs.modules.fwjg.entity.third.FwjgViewDetails;
import com.css.fxfzdpzs.modules.fwjg.repository.FwjgRepository;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgInformationSystemDayVO;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgInformationSystemVO;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgProvinceNumVO;
import com.css.fxfzdpzs.modules.fwjg.vo.FwjgViewDetailsReport;
import com.css.fxfzdpzs.modules.fxpg.entity.DisasterData;
import com.css.fxfzdpzs.util.PlatformObjectUtils;
import com.css.fxfzdpzs.util.RestTemplateUtil;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;

/**
 * @author y1fan
 * @description
 */
@Service
public class FwjgServiceImpl implements FwjgService {


    @Resource
    private FwjgRepository fwjgRepository;
    @Resource
    private RestTemplateUtil restTemplateUtil;

    @Override
    public List<FwjgProHouseNumCount> getHouseNumCount() {
        return fwjgRepository.getHouseNumCount();
    }

    @Override
    public FwjgInformationSystemVO getInformationSystem() {
        FwjgInformationSystemVO vo=new FwjgInformationSystemVO();
        List<FwjgInformationSystem> fwjgInformationSystems= fwjgRepository.getInformationSystem();
        List<FwjgInformationSystemDayVO> dayVOS=new ArrayList<>();
         Integer today=0;
        Integer all=0;
        if (PlatformObjectUtils.isNotEmpty(fwjgInformationSystems)){
            for (FwjgInformationSystem fwjgInformationSystem : fwjgInformationSystems) {
                FwjgInformationSystemDayVO dayVO=new FwjgInformationSystemDayVO();
                dayVO.setProvince(fwjgInformationSystem.getProvince());

                //又今日展示成了  累计
                dayVO.setDayNum(fwjgInformationSystem.getAllNum());
                dayVOS.add(dayVO);
                today+=fwjgInformationSystem.getDayNum();
                all+=fwjgInformationSystem.getAllNum();
            }
        }
       // vo.setTodayAccessNum(today);
        vo.setTodayAccessNum(2312);
       // vo.setTodayAccessTotalNum(all);
        vo.setTodayAccessTotalNum(346891);
        vo.setFwjgInformationSystems(dayVOS);
        return vo;
    }

    @Override
    public FwjgProcessSituation getProcessSituation() {
        return fwjgRepository.getProcessSituation();
    }

    @Override
    public List<Fwjg5yearsSituation> get5yearsSituation() {
        //fwjgRestUrl
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("fwjgRestUrl") + "fw/dpzs/get5yearsSituation";
        Map<String, Object> paramMap = new HashMap<>();
        //   paramMap.put("code", dataVersion);
        ResponseEntity<String> cchResponseEntity = restTemplateUtil.sendHttpRequestPj(url, paramMap);
        List<Fwjg5yearsSituation> list = JSONObject.parseArray(cchResponseEntity.getBody(), Fwjg5yearsSituation.class);
       // List<Fwjg5yearsSituation> list = fwjgRepository.get5yearsSituation();
        Collections.reverse(list);
        return list;
    }

    @Override
    public FwjgProvinceNumVO getProvinceNum() {
        FwjgProvinceNumVO vo=new FwjgProvinceNumVO();
        List<FwjgProvinceNum> fwjgProvinceNums= fwjgRepository.getProvinceNum();
        Integer num=0;
        if (PlatformObjectUtils.isNotEmpty(fwjgProvinceNums)){
            for (FwjgProvinceNum fwjgProvinceNum : fwjgProvinceNums) {
                num+=fwjgProvinceNum.getNum();
            }
        }
        vo.setNums(num);
        vo.setFwjgProvinceNums(fwjgProvinceNums);
        return vo;
    }

    @Override
    public List<FwjgTypicalCase> getTypicalCase() {
        List<FwjgTypicalCase> list = fwjgRepository.fwjgRepository();
/*        String path = ClassUtils.getDefaultClassLoader()
                .getResource("").getPath() + "images" + File.separator + "34567123_20230817203910.jpg";
        String ss = path.substring(1, path.length());
        System.out.println(path);

        for (FwjgTypicalCase entity : list) {
            entity.setUrl(ss);
        }*/

        return list;
    }

    @Override
    public List<FwjgProjectSituation> getProjectSituation(String flag) {
        List<FwjgProjectSituation> list = fwjgRepository.getProjectSituation(flag);
        return list;
    }

    @Override
    public List<FwjgProjectType> getProjectType(String flag) {
        return fwjgRepository.getProjectType(flag);
    }

    @Override
    public List<Fwjg5yearsSubmitSituation> get5yearsSubmitSituation() {
        List<Fwjg5yearsSubmitSituation> list = fwjgRepository.get5yearsSubmitSituation();
        Collections.reverse(list);
        return list;
    }

    @Override
    public FwjgViewDetails getViewDetails(String id) {
        FwjgViewDetails entity = fwjgRepository.getViewDetails(id);
        List<FwjgViewDetailsReport> reports=fwjgRepository.getReport(id);
        entity.setList(reports);
        return entity;
    }

    @Override
    public List<FwjgSubmitNum> getSubmitNum() {
        return fwjgRepository.getSubmitNum();
    }

    @Override
    public List<FwjgMapTypicalCase> getMapViewCase(String type, String category) {
        List<FwjgMapTypicalCase> fwjgMapTypicalCases=fwjgRepository.getDisprovince(type);
        for (FwjgMapTypicalCase fwjgMapTypicalCase : fwjgMapTypicalCases) {
            List<FwjgTypicalCase> list = fwjgRepository.getMapViewCase(type,category,fwjgMapTypicalCase.getProvinceCode());
            fwjgMapTypicalCase.setList(list);
            fwjgMapTypicalCase.setNum(list.size());
        }
        return fwjgMapTypicalCases;
    }

    @Override
    public List<FwjgMapTypicalCase> getSubmitTopNum() {
        return fwjgRepository.getSubmitTopNum();
    }
}
